#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import MySQLdb

import sys


file=sys.argv[1]
host=sys.argv[2]
port=sys.argv[3]
user=sys.argv[4]
password=sys.argv[5]
db=sys.argv[6]
currentDate=sys.argv[7]

conn = MySQLdb.connect(host,user,password,db)
cursor = conn.cursor()
cursor.execute("SET NAMES utf8")
cursor.execute("delete from nas_overview where day=%s" % currentDate )
conn.commit()

columns='cid,sid,day,flag,top100,pv'
columns_info={'cid':'str','sid':'int','day':'int','flag':'int','top100':'str','pv':'long'}

c_l=columns.strip().split(',')
args=','.join(['%s' % i for i in c_l])
args2=','.join(['%s' for i in c_l])
lines=[]
with open(file) as f:
    for i in f:
        l=i.rstrip('\n').split('\001')
        if l[0] and l[1]:
            lines.append(tuple([eval("%s('''%s''')" % (columns_info[c_l[index]],j if j else '0')) for index,j in enumerate(l)]))
sql='insert into nas_overview(%s) values(%s)' % (args,args2)
cursor.executemany(sql,tuple(lines))
conn.commit()
cursor.close()
conn.close()

